package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.CardRegister;
import com.turing.post.cardmanagement.util.CardRegisterDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "cardmanagement.impl.CardRegisterDaoImpl")
@Remote
@Local
public class CardRegisterDaoImpl implements CardRegisterDao {
	
	/**
	 * 实体管理器
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public CardRegister getCardRegisterByCardRegisterId(
			BigInteger cardRegisterId) {
		return _em.find(CardRegister.class, cardRegisterId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardRegister> getCardRegisterListByCondition(
			PCPSWebInput webInput, String cardNo, String tranType,
			String cardHolder, String startTime, String endTime,
			String saleBranchNo, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select t.* from cardregister t where 1=1 ";
		sql+=PCPSUtil.getSqlCond(webInput, saleBranchNo, "branchNo");	
		if(!PCPSUtil.isEmpty(cardNo)){
			sql+=" and t.cardno="+cardNo;
		}
		if(!PCPSUtil.isEmpty(tranType)){
			sql+=" and t.tranType="+tranType;
		}
		if(!PCPSUtil.isEmpty(cardHolder)){
			sql+=" and t.cardholder like '%"+cardHolder+"%'";
		}
		// 起始时间
		if (!PCPSUtil.isEmpty(startTime)) {
			sql = sql
					+ " and t.trantime>=to_date('"
					+ startTime + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		// 终止时间
		if (!PCPSUtil.isEmpty(endTime)) {
			sql = sql
					+ " and t.trantime<= to_date('"
					+ endTime + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		sql+=" order by t.trantime desc";
		Query query = _em.createNativeQuery(sql, CardRegister.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		List<CardRegister> list=query.getResultList();
		return list;
	}

	@Override
	public BigInteger getCardRegisterTotalByCondition(PCPSWebInput webInput,
			String cardNo, String tranType, String cardHolder,
			String startTime, String endTime, String saleBranchNo) {
		String sql = "select nvl(count(*),0) from cardregister t where 1=1";
		sql+=PCPSUtil.getSqlCond(webInput, saleBranchNo, "branchNo");	
		if(!PCPSUtil.isEmpty(cardNo)){
			sql+=" and t.cardno="+cardNo;
		}
		if(!PCPSUtil.isEmpty(tranType)){
			sql+=" and t.tranType="+tranType;
		}
		if(!PCPSUtil.isEmpty(cardHolder)){
			sql+=" and t.cardholder like '%"+cardHolder+"%'";
		}
		// 起始时间
		if (!PCPSUtil.isEmpty(startTime)) {
			sql = sql
					+ " and t.trantime>=to_date('"
					+ startTime + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		// 终止时间
		if (!PCPSUtil.isEmpty(endTime)) {
			sql = sql
					+ " and t.trantime<= to_date('"
					+ endTime + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insert(CardRegister cr) throws PCPSApplicationException {
		GeneralDao.insert(_em, cr);
	}

	@Override
	public void update(CardRegister cr) throws PCPSApplicationException {
		GeneralDao.update(_em, cr);
	}

}
